package com.math.util;

/* -------------------------
 * SimpleHistogramDemo1.java
 * -------------------------
 * (C) Copyright 2007, by Object Refinery Limited.
 *
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.SimpleHistogramBin;
import org.jfree.data.statistics.SimpleHistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A demo for the SimpleHistogramDataset class.
 * 
 * @author dgilbert
 * 
 */
public class SimpleHistogramm extends ApplicationFrame {

	public JFreeChart chart = null;
	private double MEAN = 0.5f;
	private double VARIANCE = 0.2f;
	private int width=0;
	private int height=0;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Creates a new demo.
	 * 
	 * @param title
	 *            the frame title.
	 */
	public SimpleHistogramm(String title,double mean,double variance,int width,int height) {
		super(title);
		this.MEAN=mean;
		this.VARIANCE=variance;
		this.width=width;
		this.height=height;
		JPanel chartPanel = createDemoPanel();
		chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
		setContentPane(chartPanel);
	}

	/**
	 * Creates a sample {@link HistogramDataset}.
	 * 
	 * @return the dataset.
	 */
	private IntervalXYDataset createDataset() {
		SimpleHistogramDataset dataset = new SimpleHistogramDataset(
				"Verteilung Kriterium Preis");

		double e = 0.0;
		double step = 0.01;
		for (double i = 0.0; i < (1 - step); i += step) {
			e = (i + step);
			// System.out.println(i+" - "+e);
			dataset.addBin(new SimpleHistogramBin(i, e, true, false));
		}
		dataset.addBin(new SimpleHistogramBin(e, 1.0, true, true));

		// dataset.addBin(new SimpleHistogramBin(0.0, 0.1, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.1, 0.2, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.2, 0.3, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.3, 0.4, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.4, 0.5, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.5, 0.6, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.6, 0.7, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.7, 0.8, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.8, 0.9, true, false));
		// dataset.addBin(new SimpleHistogramBin(0.9, 1.0, true, true));

		GaussianNumberGenerator gaussian = new GaussianNumberGenerator();

		for (int i = 1; i <= 10000; i++) {
			// double value=gaussian.getRandomGaussianValue(MEAN, VARIANCE);
			double value = gaussian.getRandomGaussianValueRange(MEAN, VARIANCE,
					0.0, 1.0);
			dataset.addObservation(value);
		}

		return dataset;
	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            a dataset.
	 * 
	 * @return The chart.
	 */
	private JFreeChart createChart(IntervalXYDataset dataset) {
		this.chart = ChartFactory.createHistogram("Distribution", null, null,
				dataset, PlotOrientation.VERTICAL, true, true, false);
		return this.chart;
	}

	public void drawChart(JFreeChart chart) {
		BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.orange);
		g.drawRect(0, 0, width-10, height-10);

		Rectangle drawArea = new Rectangle(width, height);
		chart.draw(g, drawArea);

		try {
			FileOutputStream fos = new FileOutputStream("G:\\test.jpeg");
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			ImageIO.write(image, "jpeg", bos);
			bos.close();
		} catch (FileNotFoundException e) {
			System.out.println("file not found " + e.getMessage());
			System.exit(1);
		} catch (IOException e) {
			System.out.println("IOException " + e.getMessage());
			System.exit(1);
		}

	}
	
	public BufferedImage getChartImage(JFreeChart chart) {
		BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.orange);
		g.drawRect(0, 0, width-10, height-10);

		Rectangle drawArea = new Rectangle(width, height);
		chart.draw(g, drawArea);

		return image;

	}
	

	/**
	 * Creates a panel for the demo (used by SuperDemo.java).
	 * 
	 * @return A panel.
	 */
	public JPanel createDemoPanel() {
		JFreeChart chart = createChart(createDataset());
		return new ChartPanel(chart);
	}

	/**
	 * The starting point for the demo.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(String[] args) {
		SimpleHistogramm demo = new SimpleHistogramm("Distribution",0.5,0.2,500,270);
		//demo.getChartImage(demo.chart);
		//demo.drawChart(demo.chart);
		 demo.pack();
		 RefineryUtilities.centerFrameOnScreen(demo);
		 demo.setVisible(true);
	}

}